import java.util.HashSet;
import java.util.Set;

public class Solution720 {
    public String longestWord(String[] words) {
        Set<String> stringSet=new HashSet<>();
        for (String word : words) {
            stringSet.add(word);
        }
        String ans="";
        for (String s : stringSet) {
            boolean flag=true;
            for (int i=1;i<s.length();i++){
                if (!stringSet.contains(s.substring(0,i))){
                    flag=false;
                    break;
                }
            }
            if (flag && (s.length()>ans.length() || s.length()==ans.length() && s.compareTo(ans)<0)){
                ans=s;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution720().longestWord(new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"}));
    }
}
